//
//  HRMainTabBarController.swift
//  weiBo
//
//  Created by helong on 16/7/9.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit
import SVProgressHUD

class HRMainTabBarController: UITabBarController {
    //  懒加载遮罩视图
//    private lazy var cover: HRComposeView = {
//        let view = HRComposeView()
//        return view
//    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        let hrTabBar = HRTabBar()
//        hrTabBar.opaque = true
        //设置代理属性
        hrTabBar.tarBarDelegate = self
        
        hrTabBar.addButtonClickClosure = {
           //  中间按钮的点击事件
            //  如果是已经登录的状态
            if HRUserAccountInfos.sharedUserAccount.isLogin {
                
//                self.setupComposeView()
                 let cover = HRComposeView()
                cover.show(self)
                
            }else{//    如果是没有登陆的额状态
                SVProgressHUD.showErrorWithStatus("您还没有登录哦,亲!", maskType:.Black)

            }
   
        }
//        self.tabBar = hrTabBar//不能给只读属性赋值
        //使用KVC在运行时给只读属性赋值
        setValue(hrTabBar, forKey: "tabBar")
       
        /**如下代码可以在下面重载的方法中实现
        
        let homeTab = HRHomeTableViewController()
        let homeNav = UINavigationController(rootViewController: homeTab)
        homeTab.title = "首页"
        homeTab.tabBarItem.image = UIImage(named: "tabbar_home")
        homeTab.tabBarItem.selectedImage = UIImage(named: "tabbar_home_selected")
*/
        
        //使用封装的方法给tabBar添加四个页面
        addChildViewController(HRHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(HRMessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        addChildViewController(HRDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(HRProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        

        
    }
    
     // MARK: - 给compose视图添加视图和约束
    private func setupComposeView(){
       guard let window = UIApplication.sharedApplication().windows.last else {
            return
        }
        //  注意:这个空间不能懒加载,否则每次的截屏都一样
        let cover = HRComposeView()
        window.addSubview(cover)

        
    }
    //MARK: - 重载父类方法设置tabBar
    /**
     重载父类的addChildViewController方法,将每个模块的tableviewcontroller包装在导航栏时候,添加到tabBarController中
     
     - parameter childController: 每个模块的tableviewcontroller
     - parameter title:           导航栏 和 tabBar的标题
     - parameter imageName:       tabBar的图片
     */
    func addChildViewController(childController: UIViewController,title: String,imageName: String) {
        
        childController.tabBarItem = HRtabBarIten()
        childController.title = title
//        childController.tabBarItem.setTitleTextAttributes([NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle)]) , forState: .Normal)
        //设置tabBar的文字的大小,颜色
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(13), NSForegroundColorAttributeName : UIColor.darkGrayColor()], forState: .Normal)
        
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(13), NSForegroundColorAttributeName : UIColor.orangeColor()], forState:.Selected)
        //拼接高亮图片的名称
        let seletedImageName = imageName + "_selected"
      //给tabBar设置图片,并设置图片的渲染模式
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode((.AlwaysOriginal))
        childController.tabBarItem.selectedImage = UIImage(named: seletedImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        //新建导航栏控制器,并设置器根控制器
        let navigationController = UINavigationController(rootViewController: childController)
        //将导航控制器添加到tabBar中
        addChildViewController(navigationController)
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// MARK: - 代理传递中间按钮的点击事件
extension HRMainTabBarController:addButtonDelegate{
    
    func addButtonClicked() {
        print("我是代理传递过来的中间按钮的点击事件")
        printLog()
    }
    
}
